package com.biqee.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.biqee.dao.BqEstimateResultDAO;
import com.biqee.dao.base.BaseDAOImpl;
import com.biqee.model.db.BqEstimateResult;
import com.biqee.model.db.BqEstimateResultQueryView;

public class BqEstimateResultDAOImpl extends BaseDAOImpl implements BqEstimateResultDAO {

	@Override
	public Integer create(BqEstimateResult bqEstimateResult) {
		if(bqEstimateResult == null) {
			throw new RuntimeException("bqEstimateResult should not be null");
		}
		
		return (Integer)getSqlMapClientTemplate().insert(getRealSqlId("create"), bqEstimateResult);
	}
	
	@Override
	public void deleteByBqTuanId(Integer bqTuanId) {
		getSqlMapClientTemplate().delete(getRealSqlId("deleteByBqTuanId"), bqTuanId);
	}

	@Override
	public BqEstimateResult read(Integer bqTuanId, String site, String siteItemId) {
		Map<String, Object> condition = new HashMap<String, Object>();
		condition.put("bqTuanId", bqTuanId);
		condition.put("site", site);
		condition.put("siteItemId", siteItemId);
		
		return (BqEstimateResult)getSqlMapClientTemplate().queryForObject(getRealSqlId("readBySiteItem"), condition);
	}

	@Override
	public int updateScoreById(int score, Integer id) {
		Map<String, Integer> condition = new HashMap<String, Integer>();
		condition.put("score", score);
		condition.put("id", id);
		
		return getSqlMapClientTemplate().update(getRealSqlId("updateScoreById"), condition);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<BqEstimateResultQueryView> readMoreThanList(int score, int start,
			int number, int bqTuanId) {
		Map<String, Object> condition = new HashMap<String, Object>();
		condition.put("score", score);
		condition.put("start", start);
		condition.put("number", number);
		condition.put("bqTuanId", bqTuanId);

		return (List<BqEstimateResultQueryView>)getSqlMapClientTemplate().queryForList(getRealSqlId("readByScoreMoreThan"), condition);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<BqEstimateResultQueryView> readLessThanList(int score,
			int start, int number, int bqTuanId) {
		Map<String, Object> condition = new HashMap<String, Object>();
		condition.put("score", score);
		condition.put("start", start);
		condition.put("number", number);
		condition.put("bqTuanId", bqTuanId);

		return (List<BqEstimateResultQueryView>)getSqlMapClientTemplate().queryForList(getRealSqlId("readByScoreLessThan"), condition);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<BqEstimateResultQueryView> readBetweenList(int lowScore,
			int highScore, int start, int number, int bqTuanId) {
		Map<String, Object> condition = new HashMap<String, Object>();
		condition.put("lowScore", lowScore);
		condition.put("highScore", highScore);
		condition.put("start", start);
		condition.put("number", number);
		condition.put("bqTuanId", bqTuanId);

		return (List<BqEstimateResultQueryView>)getSqlMapClientTemplate().queryForList(getRealSqlId("readByScoreBetween"), condition);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<BqEstimateResult> readBqEstimateResultsByBqTuanId(
			Integer bqTuanId) {
		return (List<BqEstimateResult>)getSqlMapClientTemplate().queryForList(getRealSqlId("readBqEstimateResultsByBqTuanId"), bqTuanId);
	}

	
}
